import { getIndexData,getCategoryList } from "@/service/index.js";
export const haeadmixins = {
	data() {
		return {
			list:[],
			getHeadPotraitArr:[],
			tabIndex:0,
			page:1,
			limit:20,
			id:'',
			showLogin:false
		}
	},
	created() {
		this.getCategoryList(this.type).then(res =>{
			this.HeadPotraitArrInit()
		})
	},
	//监听用户上拉刷新
	onReachBottom(){
		console.log('用户上拉加载了');
		this.page++
		this.getHeadPotrait(this.id,true)
	},
	methods: {
		
		//获取所有分类
		getCategoryList(type){
			return getCategoryList().then(res => {
				let categorylist = []
				if(this.type == "头像"){
					categorylist = res.data[0].children
				}else if(this.type == "壁纸"){
					categorylist = res.data[1].children
				}
				categorylist.forEach((item,index,arr) => {
					item.name = item.categoryName
				})
				this.list = categorylist
			})
		},
		
		//头像数据
		HeadPotraitArrInit() {
			this.list.forEach(item => {
				let HeadPotraitItem = {
					id: item.id,
					page:1,
					HeadPotraitList: []
				}
				this.getHeadPotraitArr.push(HeadPotraitItem)
			})
			this.id = this.list[0].id
			this.getHeadPotrait(this.list[0].id)
		},
		getHeadPotrait(id,onReachBottom) {
			
			this.getHeadPotraitArr.forEach((item, index) => {
				if (item.id == id && (item.HeadPotraitList.length == 0 || onReachBottom)) {
					if(this.getHeadPotraitArr[index].page<=this.page){
						this.getHeadPotraitArr[index].page = this.page
					}
					getIndexData(id,this.getHeadPotraitArr[index].page,this.limit).then(res=>{
						let headPotrait = res
						this.showLogin = true
						this.getHeadPotraitArr[index].HeadPotraitList = [...this.getHeadPotraitArr[index].HeadPotraitList,...headPotrait.data[id]]
					})
				}
			})
			console.log('处理完成的头像数组', this.getHeadPotraitArr);
		},
		goImgBox(imageSrc) {
			this.$link.goTo('/common/pages/img-box?imgSrc=' + encodeURIComponent(imageSrc) + '&type=头像')
		},
		tabsChange(e) {
			this.showLogin = false
			console.log('返回的e',e);
			this.page = 1
			this.tabIndex = e.index
			this.id = e.id
			this.getHeadPotrait(e.id)
			if(this.type == '壁纸'){
				this.$refs.waterfallsFlowRef.refresh();
			}
		}
	}
};
